package com.mos.dao.impl;

import com.mos.base.sql.BaseDaoImpl;
import com.mos.dao.MenuDao;
import com.mos.entity.Menu;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * Package Name: com.mos.dao.impl
 * User: grq
 * Date: 2016/3/29
 * Time: 10:29
 * Description:some des here!
 */
@Repository
public class MenuDaoImpl extends BaseDaoImpl implements MenuDao {

    public int save(Menu menu) {
        Assert.notNull(menu,"menu entity can't be null !");

        String sql = "insert into t_menu (id,name,pid,traceIds,path,icon,permission,isShow,sortNo,description," +
                "createTime,createrId,updateTime,updaterId,del) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        Object[] params = {menu.getId(),menu.getName(),menu.getPid(),menu.getTraceIds(),menu.getPath(),menu.getIcon(),
                menu.getPermission(),menu.getIsShow(),menu.getSortNo(),menu.getDescription(),menu.getCreateTime(),
                menu.getCreaterId(),menu.getUpdateTime(),menu.getUpdaterId(),menu.getDel()};
        return super.addOrUpdateOrDelete(sql,params,Menu.class);
    }

    public List<Map<String, Object>> queryList(Object[] params) {
        String sql = "select * from t_menu order by sortNo desc ";
        return super.queryList(sql,params);
    }

    public Menu findById(String id) {
        String sql = "select * from t_menu where id = ? order by sortNo desc ";
        return  super.findForObject(sql,new Object[]{id},Menu.class);
    }

    public List<Map<String, Object>> queryListByPid(String pid){
        String sql = "select * from t_menu where pid = ? order by sortNo desc ";
        Object[] params = {pid};
        return super.queryList(sql,params);

    }

    public Map<String, Object> getRoot() {
        String sql = "select * from t_menu where pid is null ";
        return  super.find(sql,null);
    }
}
